export default {
  namespaced: true,
  state: () => ({
    postitem: JSON.parse(uni.getStorageSync('postitem') || '[]'),
    deleteId: JSON.parse(uni.getStorageSync('deleteId') || '[]'),
    isDot: uni.getStorageSync('isDot') || false// 红点显示
    // deleteId: JSON.parse(uni.getStorageSync('deleteId') || '[]')
  }),
  mutations: {
    // 修改显示红点状态
    updateIsDot(state,isDot) {
      state.isDot = isDot
      this.commit('postitem_module/saveIsDot')
    },
    saveIsDot(state) {
      uni.setStorageSync('isDot',state.isDot)
    },
    savePostItemStorage(state) {
      uni.setStorageSync('postitem',JSON.stringify(state.postitem))
    },
    saveDeletedIdStorage(state) {
      uni.setStorageSync('deleteId',JSON.stringify(state.deleteId))
    },
    addPostItem(state,item) {
      state.postitem.push(item)
      this.commit('postitem_module/savePostItemStorage')
    },
    updatePostItem(state,postList) {
      state.postitem = postList
      this.commit('postitem_module/savePostItemStorage')
    },
    removePostItemById(state,id) {
      state.postitem = state.postitem.filter(x => x.serialNumber !== id)
      this.commit('postitem_module/savePostItemStorage')
    },
    updateState(state,item) {
      const results = state.postitem.find(x => x.serialNumber === item.serialNumber)
      results.isSelected = item.isSelected
      this.commit('postitem_module/savePostItemStorage')
    },
    selectDelectId(state) {
      // let num = []
      for(let i = 0;i < state.postitem.length;i++) {
        if(state.postitem[i].isSelected === true) {
          state.deleteId.push(state.postitem[i].serialNumber)
          // num.push(i)
        }
      }
      // for(let i = 0;i < num.length;i++) {
      //   state.postitem.splice(num[i],1)
      // }
      // this.commit('postitem_module/savePostItemStorage')
      this.commit('postitem_module/saveDeletedIdStorage')
    },
    clearDeleteId(state) {
      state.deleteId = []
      this.commit('postitem_module/saveDeletedIdStorage')
    },
    selectRestore(state,item) {
      for(let i = 0;i < item.length;i++) {
              if(item[i].isSelected === true) {
                state.deleteId.push(item[i].serialNumber)
              }
           }
      },
    changeChecked(state,newCheck) {
      state.postitem.forEach(x => x.isSelected = newCheck)
      this.commit('postitem_module/saveDeletedIdStorage')
    }
  },
  getters: {
    
  }
}